﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Drawing;
using System.IO;
using PTCapture.Support;

namespace PTCapture.PatientInfo
{
    /// <summary>
    /// For serialization
    /// </summary>
    [Serializable()]
    [XmlRoot("ExerciseList")]
    public class ExerciseList
    {
        [XmlArray("Exercises")]
        [XmlArrayItem("Exercise", typeof(Exercise))]
        public List<Exercise> Items { get; set; }
    }

    [Serializable()]
    public class Exercise
    {
        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Description")]
        public string Description { get; set; }

        [XmlElement("ExampleImageName")]
        public string ExampleImageName { get; set; }

        [XmlElement("ExampleImage")]
        public string ExampleImage { get; set; }

        public Exercise()
        {
        }

        public Exercise(string name, string description, string exampleImageName, string exampleImage)
        {
            Name = name;
            Description = description;
            ExampleImageName = exampleImageName;
            ExampleImage = exampleImage;
        }
    }

    public static class ExerciseSupport
    {
        #region XML

        /// <summary>
        /// Save exercise class as XML.
        /// </summary>
        public static void SaveExerciseXML()
        {
            ExerciseList pl = new ExerciseList();
            pl.Items = GlobalVariables.Exercises;

            XmlSerializer serializer = new XmlSerializer(typeof(ExerciseList));

            using (FileStream fs = new FileStream(App.Default.RootPath + "\\Exercises.xml", FileMode.Create))
            {
                serializer.Serialize(fs, pl);
            }
        }

        /// <summary>
        /// Load existin exercises from XML.
        /// </summary>
        public static void LoadExerciseXML()
        {
            if (File.Exists(App.Default.RootPath + "\\Exercises.xml"))
            {
                ExerciseList pl = new ExerciseList();

                XmlSerializer serializer = new XmlSerializer(typeof(ExerciseList));

                using (FileStream fs = new FileStream(App.Default.RootPath + "\\Exercises.xml", FileMode.Open))
                {
                    pl = (ExerciseList)serializer.Deserialize(fs);
                }

                GlobalVariables.Exercises = pl.Items.OrderBy(w => w.Name).ToList();
            }
        }

        #endregion
    }
}
